#include <iostream>
#include <mysql.h>
#include <vector>
#include <string>
using namespace std;

#define IYOGO_MAIN

#include "Config.h"
#include "Card.h"
#include "Connector.h"
#include "Customer.h"
#include "Adaptor.h"
class Action
{
public:
    virtual void execue(istream&,Connector&)=0 ;
};

class AddMember:public Action
{
    void execue(istream&,Connector&);
};

void AddMember::execue(istream & in, Connector & conn)
{
    string name;
    string job;
    string address;
    in>>name>>address>>job;
    Customer customer(0,name,job,address,1);
    Adaptor<Customer> adaptor(conn);
    adaptor.Insert(customer);
    int customid=adaptor.Getid(name);
    cout<<"OK."<<name<<"'s member id is "<<customid<<"."<<endl;
}


class AddCard:public Action
{
    void execue(istream&,Connector&);
};

void AddCard::execue(istream & in, Connector & conn)
{
    string name;
    string typesname;

    cin>>name>>typesname;
    int type=typesname=="年卡"?0:1;
    int count=INT32_MAX;
    if(type)
        cin>>count;

    Date activetime;
    Date inactivetime;
    Card card(0,type,count,activetime,inactivetime);
    Adaptor<Card> cardadaptor(conn);
    Adaptor<Customer> customeradptor(conn);

    cardadaptor.Insert(card);

    int customid=customeradptor.Getid(name);
    int cardid=cardadaptor.GetLastid();

    if(!customid)
        cout<<"Sorry, no customer named "<<name<<"."<<endl;
    customeradptor.BindCard(customid,cardid);
    if(!type)
        cout<<"OK."<<"A new"<<typesname<<" added to "<<name<<"("<<customid<<")."<<endl;
    else
        cout<<"OK."<<"A new"<<typesname<<" with "<<count<<" times added to "<<name<<"."<<endl;
}


Action* ReadCommand(istream& in)
{
    string temp;
    string command;
    while(in>>temp) {
        if (temp != "iyoga") {
            cerr << "Error" << "  command" << endl;
            return NULL;
        }
        in >> command;
        if (command=="add-member")
        {
            return new AddMember;
        }
        else if (command=="add-card")
        {
            return new AddCard;
        }
        else if (command=="add-course")
        {
            //TODO:添加课程,实现对应接口,并调用接口
        }
        else if(command=="add-coach")
        {
            //TODO:添加教练,同上
        }
        else if(command=="schedule")
        {
            //TODO:绑定课程和教练,注意select课程和select教练时检查是否存在
        }
        else if(command=="subscribe")
        {
            //TODO:同上,注意检查是否存在
        }
        else if(command=="cancel")
        {
            //TODO:退订课程,注意检查时间(在Date类中实现相关函数),如果符合条件,更新订阅状态
        }
        else
        {
            cout<<"Don't support function"<<endl;
            return NULL;
        }
        return NULL;
    }
}

int main() {
    //cout<<Config::passwd<<endl;
    Connector conn;
    conn.Connect(Config::host,Config::user,Config::passwd,Config::db,Config::port);

    Action* action;
    while((action=ReadCommand(cin)))
        action->execue(cin,conn);


    return 0;
}